let express = require('express');
//import express from 'express';
//var mysqlData = require('./test');
let bodyParser = require("body-parser");
let app = express();
let fs = require("fs");
const cookieParser = require('cookie-parser');


let signStr = 'xadsafeowirw';
app.use(cookieParser(signStr));

//设置跨域访问
app.all('*', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1');
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});

//设置访问文件格式
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({extended: true})); // for parsing application/x-www-form-urlencoded

//配置服务端口
let server = app.listen(8080, function () {

    let host = server.address().address;

    let port = server.address().port;

    console.log("应用实例，访问地址为 http://%s:%s", host, port);
});

let questions = [
    {
        data: 213,
        tel: 15982086412,
        age: 12,
        item: [
            '仙女',
            '初恋',
            '忘不掉'
        ]
    },
    {
        data: 456,
        tel: 15982086413,
        age: 13,
        item: [
            '真爱',
            '心痛',
            '一直想'
        ]
    }];
//添加的新用户数据
let user = {
    "user4": {
        "name": "mohit",
        "password": "password4",
        "profession": "teacher",
        "id": 4
    }
};

function fibonacci(n) {
    if (n <= 1) return 1;
    return fibonacci(n - 1) + fibonacci(n - 2);
}

function Fibonacci(n, ac1 = 1, ac2 = 1) {
    if (n <= 1) return ac2;
    return Fibonacci(n - 1, ac2, ac1 + ac2);
}

/*随机字符*/
function uuid(len, radix) {
    let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
    let uuid = [];
    radix = radix || chars.length;

    if (len) {
        for (let i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
    } else {
        let r;

        uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
        uuid[14] = '4';

        for (let i = 0; i < 36; i++) {
            if (!uuid[i]) {
                r = 0 | Math.random() * 36;
                uuid[i] = chars[(i === 19) ? (r & 0x3) | 0x8 : r];
            }
        }
    }

    return uuid.join('');
}

/*端口默认访问地址*/
app.get('/', function (req, res) {
    /*res.cookie('name', uuid(15));
    console.log(req.cookies);*/
    console.log('初始');
    res.set('Content-Type', 'text/html');
    res.sendFile(__dirname + '/HTML/index.html');
});

//写个接口123get
app.get('/123', function (req, res) {
    console.log("确认");
    console.log(req.query);
    let num = Fibonacci(req.query.num);
    console.log(num);
    res.status(200);
    res.json(num);
});
//写个接口123post
app.post('/chun_bao', function (req, res) {
    questions.forEach(function (item) {
        if (item.tel === req.body.tel) {
            console.log('接口chun_bao');
            let inde = [item];
            res.status(200);
            res.json(inde);
        }
    });
});

//用于读取指定用户的详细信息
app.get('/index/:id', function (req, res) {
    // 首先我们读取已存在的用户
    fs.readFile(__dirname + "/" + "users.json", 'utf8', function (err, data) {
        data = JSON.parse(data);
        let user = data["user" + req.params.id];
        console.log('读取详细');
        res.end(JSON.stringify(user));
    });
});